package cn.itcast.search.listen;

import cn.itcast.search.constatnts.MqConstatnts;
import cn.itcast.search.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author oldwine
 * @date 2022/6/22
 * @Version 1.0
 */
@Component
@Slf4j
public class ItemQueueListener {
    @Autowired
    private SearchService searchService;

    /**
     * 监听商品上架方法
     * @param itemId 商品id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(MqConstatnts.ITEM_UP_QUEUE),
            exchange = @Exchange(value = MqConstatnts.ITEM_EXCHANGE,type = ExchangeTypes.TOPIC),
            key = MqConstatnts.ITEM_INSERT_KEY
    ))
    public void listenUpQueue(Long itemId){
        log.info("接收到商品上架  消息内容==>{}",itemId);
        //调用添加商品的方法
        searchService.insertById(itemId);
    }

    /**
     * 监听商品下架方法
     * @param itemId 商品id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(MqConstatnts.ITEM_DOWN_QUEUE),
            exchange = @Exchange(value = MqConstatnts.ITEM_EXCHANGE,type = ExchangeTypes.TOPIC),
            key = MqConstatnts.ITEM_DELETE_KEY
    ))
    public void listenDownQueue(Long itemId){
        log.info("接收到商品下架 消息内容==>{}",itemId);
        //调用删除商品方法
        searchService.deleteById(itemId);
    }
}
